package com.inspur.gs.sbssdl.sbyxgl.djjl.djjl.back.bizactions;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.api.services.IBefSessionManager;
import com.inspur.edp.cef.api.message.*;
import com.inspur.edp.bef.api.action.determination.*;
import com.inspur.edp.bef.api.action.validation.*;
import com.inspur.edp.cef.entity.changeset.*;
import com.inspur.edp.bef.api.be.*;
import com.inspur.edp.bef.api.action.VoidActionResult;
import com.inspur.edp.bef.spi.action.AbstractManagerAction;
import com.inspur.edp.bef.core.LcpUtil;
import com.inspur.edp.bef.spi.action.AbstractManagerAction;
import com.inspur.edp.cef.entity.changeset.ModifyChangeDetail;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.svc.document.storage.api.DocStorageService;
import com.inspur.edp.svc.document.storage.entity.GspDocMetadata;
import com.inspur.edp.svc.document.storage.entity.GspDocOperatingModes;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import io.iec.edp.caf.rpc.client.RpcClassHolder;

import java.io.ByteArrayOutputStream;
import java.math.BigDecimal;
import java.time.Duration;
import java.util.*;
import java.util.ArrayList;

public class sbdjjluploadFjMgrAction extends AbstractManagerAction<String> {
	private String id;
	private ArrayList<Object> picList;
	public sbdjjluploadFjMgrAction(IBEManagerContext managerContext, String id, ArrayList<Object> picList) {
		super(managerContext);
		this.id = id;
		this.picList = picList;
	}

	private String mxCfg = "com.inspur.gs.sbssdl.sbwhgl.sbrh.sbrh.back.sbssSbrhMx";

	@Override
	public  void execute() {
		String rootId = "maintenance";
		RpcClassHolder rpcClassHolder = SpringBeanUtils.getBean(RpcClassHolder.class);
		DocStorageService docStorage = rpcClassHolder.getRpcClass(DocStorageService.class);
		try {
			SpringBeanUtils.getBean(IBefSessionManager.class).createSession(Duration.ofMinutes(5));
			IStandardLcp mxLcp = LcpUtil.getLcp(mxCfg);
			IEntityData data = mxLcp.retrieve(id).getData();
			String djjg = data.getValue("djjg").toString();
			String djbw = data.getValue("djbw").toString();
			String fileName = djjg + "_" + djbw;
			int index = 0;
			for (Object pic : picList) {
				index++;
				List<Integer> picList = (List<Integer>) pic;
				// 获取前端传递的图片流 转成 byte[]
				Byte[] sbbhs = new Byte[picList.size()];
				for (int i = 0; i < picList.size(); i++) {
					sbbhs[i] = (byte) (picList.get(i) & 0xFF);
				}

				GspDocMetadata metadata = new GspDocMetadata(rootId);
				metadata.setFileName(fileName + "_" + index);
				metadata.setDocType("png");
				metadata.setDocSize(String.valueOf(sbbhs.length));
				byte[] bytes = toPrimitives(sbbhs);
				String attachmentId = docStorage.uploadDoc(fileName, metadata, bytes, GspDocOperatingModes.Formal);

				ArrayList<String> nodeCodes = new ArrayList<>();
				nodeCodes.add("djfj");
				ArrayList<String> hierarchyIds = new ArrayList<>();
				hierarchyIds.add(id);
				IEntityData fjInfo = mxLcp.retrieveDefaultChild(nodeCodes, hierarchyIds);

				HashMap<String, Object> map = new HashMap<>();
				map.put("AttachmentId", attachmentId);
				map.put("FileName", metadata.getFileName());
				map.put("FileSize", new BigDecimal(metadata.getDocSize()));
				map.put("FileCreateTime", new Date());

				ModifyChangeDetail dataChange = new ModifyChangeDetail(id);
				ModifyChangeDetail mxChange = new ModifyChangeDetail(fjInfo.getID());
				mxChange.putMultiUdtChange("fjInfo", map);

				dataChange.addChildChangeSet("djfj", mxChange);

				mxLcp.modify(dataChange);
			}
			mxLcp.save();
			setResult("上传成功");

		} catch (Exception e) {
			e.printStackTrace();
			setResult("上传失败");
		} finally {
			SpringBeanUtils.getBean(IBefSessionManager.class).closeCurrentSession();
		}

	}
	private IBusinessEntity getEntity(String dataId) {
		return (IBusinessEntity)super.getBEManagerContext().getEntity(dataId);
	}	private IBEService getMgr() {
		return (IBEService)super.getBEManagerContext().getBEManager();
	}
	public byte[] toPrimitives(Byte[] oBytes) {
		return Arrays.stream(oBytes).mapToInt(Byte::intValue) // 先转 int
				.collect(ByteArrayOutputStream::new, (baos, val) -> baos.write((byte) val), (baos1, baos2) -> {
				}).toByteArray();
	}
}